package com.springboot.cloud.service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import com.springboot.cloud.dao.UserMapper;


@Service
public class UserService implements UserDetailsService{
	@Autowired
	UserMapper usermapper;
	@Override
	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
		
		com.springboot.cloud.entities.User user = usermapper.getUserByName(username);
		
        if (user == null) {
            throw new UsernameNotFoundException("用户名不存在");
        }
        String role = user.getRole();

       
        List<GrantedAuthority> authorities = new ArrayList<>();
        
        authorities.add(new SimpleGrantedAuthority(role));

       User u =new User(user.getUsername(),user.getPassword(), authorities);
       System.out.println(u.toString());
        return  u;
	}
	
	public com.springboot.cloud.entities.User getuser(String username){
		
		com.springboot.cloud.entities.User user = usermapper.getUserByName(username);
		System.out.println(user.toString());
		return user;
		
	}

	public Integer getUserid(String username) {
		return usermapper.getUserByName(username).getId();
	}
}
